<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Crypt\Driver\Think;

class GoodsController extends CommonController {
    public function index(){
        $where = array();
        //搜索
        $good_name = I('good_name');
        if($good_name){
            $where['good_name'] =array('like',"%$good_name%");
        }
        $row = 10;
        $page = $_GET['p'] ? $_GET['p'] : 1;
        $count = M('goods g')->field('g.*,gc.good_cate_name')
            ->join('__GOOD_CATE__ gc on gc.id=g.cate_id')
            ->where($where)->count();
        $Page = new \Think\Page($count,$row);
        $show = $Page->show();
        $list = M('goods g')->field('g.*,gc.good_cate_name')
            ->join('__GOOD_CATE__ gc on gc.id=g.cate_id')
            ->where($where)->page($page,$row)->order('good_id desc')->select();
        $this->assign('good_name',$good_name);
        //查询商品包装单位
        $pack_unit = M('system_config')->where(array('config_id'=>4))->getField('config_value');

        //查询分类
        $cate = M('good_cate')->where('good_parent_id!=0')->select();
        $this->assign('cate',$cate);
        $this->assign('pack_unit',$pack_unit);
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }

    //添加
    public function add(){
        if(IS_POST){
            if($_FILES){
                $picture = upload();
                $data['picture'] = $picture['picture'];
                unset($picture['picture']);
                $data['assist_picture'] = json_encode($picture);
            }
            $data['good_name'] = I('good_name');
            $data['cate_id'] = I('cate_id');
            $data['price'] = I('price');
            $data['vip_price'] = I('vip_price');
            $data['good_code'] = I('good_code');
            $data['grade'] = I('grade');
            $data['pack_length'] = I('pack_length');
            $data['pack_width'] = I('pack_width');
            $data['pack_height'] = I('pack_height');
            $data['alone_install'] = I('alone_install');
            if(I('alone_install') == 1){
                $data['alone_install_price'] = I('alone_install_price');
            }
            $data['desc'] = I('desc');
            if(M('goods')->add($data)){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败');
            }

        }

        //查询商品包装单位
        $pack_unit = M('system_config')->where(array('config_id'=>4))->getField('config_value');

        //查询二级分类
        $cate = M('good_cate')->field('id,good_cate_name')->where('good_parent_id!=0')->select();
        $this->assign('pack_unit',$pack_unit);
        $this->assign('cate',$cate);
        $this->display();
    }

    //删除
    public function  del(){
        $id = I('uid');
        if(M('goods')->where('good_id='.$id)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('数据异常');
        }
    }

    //编辑
    public function edit(){
        $id = I('uid');
        $info = M('goods')->where('good_id='.$id)->find();
        //将商品辅图转为数组
        $info['assist_picture'] = json_decode($info['assist_picture'],true);

        if(IS_POST){
            //开启事务
            M()->execute('begin');
            $id = I('uid');
            if($_FILES){
                $picture = upload();
                foreach ($picture AS $k => $v){
                    if($k == 'picture' && $picture[$k]){
                        unlink(ltrim($info['picture'],'/'));
                        $data['picture'] = $picture['picture'];
                        unset($picture['picture']);
                    }
                    //判断辅图是否修改
                    if($picture[$k]){
                        unlink(ltrim($info['assist_picture'][$k],'/'));
                        $info['assist_picture'][$k] = $picture[$k];
                        $data['assist_picture'] = json_encode($info['assist_picture']);
                    }
                }
            }
            $data['good_name'] = I('good_name');
            $data['cate_id'] = I('cate_id');
            $data['price'] = I('price');
            $data['vip_price'] = I('vip_price');
            $data['good_code'] = I('good_code');
            $data['grade'] = I('grade');
            $data['pack_length'] = I('pack_length');
            $data['pack_width'] = I('pack_width');
            $data['pack_height'] = I('pack_height');
            $data['alone_install'] = I('alone_install');
            if(I('alone_install') == 1){
                $data['alone_install_price'] = I('alone_install_price');
            }
            $data['desc'] = I('desc');
            if(false === M('goods')->where('good_id='.$id)->save($data)){
                M()->execute('rollback');
                $this->error('修改失败');
            }else{
                M()->execute('commit');
                $this->success('修改成功',U('index'));
            }
        }

        //查询二级分类
        $cate = M('good_cate')->field('id,good_cate_name')->where('good_parent_id!=0')->select();

        //查询商品包装单位
        $pack_unit = M('system_config')->where(array('config_id'=>4))->getField('config_value');
        $this->assign('pack_unit',$pack_unit);
        $this->assign('info',$info);
        $this->assign('cate',$cate);
        $this->display();
    }
    
    /*
	* 方法: is_new
	* 功能：设置新品
	* 传参：good_id
    *       type,0为取消新品，1为设置新品
	**/
    public function is_new()
    {
        //查询该商品新品状态，对值取反
        if(I('type') == 0){
            $data['is_new'] = 0;
        }else{
            $data['is_new'] = 1;
        }
        $goodInfo = M('goods')->where(array('good_id' => I('good_id')))->save($data);
        if($goodInfo === false)
        {
            $this->error('保存失败');
        }else{
            $this->success('保存成功');
        }
    }
}